package com.ruoyi.zcxt.service.impl;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zcxt.constant.ZcxtConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zcxt.mapper.ZcUseMapper;
import com.ruoyi.zcxt.domain.ZcUse;
import com.ruoyi.zcxt.service.IZcUseService;

/**
 * 资产用途（树）Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-06
 */
@Service
public class ZcUseServiceImpl implements IZcUseService 
{
    @Autowired
    private ZcUseMapper zcUseMapper;

    /**
     * 查询资产用途（树）
     * 
     * @param useId 资产用途（树）主键
     * @return 资产用途（树）
     */
    @Override
    public ZcUse selectZcUseByUseId(Long useId)
    {
        return zcUseMapper.selectZcUseByUseId(useId);
    }

    /**
     * 查询资产用途（树）列表
     * 
     * @param zcUse 资产用途（树）
     * @return 资产用途（树）
     */
    @Override
    public List<ZcUse> selectZcUseList(ZcUse zcUse)
    {
        return zcUseMapper.selectZcUseList(zcUse);
    }

    /**
     * 新增资产用途（树）
     * 
     * @param zcUse 资产用途（树）
     * @return 结果
     */
    @Override
    public int insertZcUse(ZcUse zcUse)
    {
        ZcUse info = zcUseMapper.selectZcUseByUseId(zcUse.getParentId());
        if (info == null && zcUse.getParentId() == 0) {
            // 父节点直接增加
            zcUse.setAncestors("0");
        }else {
            // 如果父节点不为正常状态,则不允许新增子节点
            if (!ZcxtConstants.USE_NORMAL.equals(info.getStatus())) {
                throw new ServiceException("用途停用，不允许新增");
            }
            zcUse.setAncestors(info.getAncestors() + "," + zcUse.getParentId());
        }
        zcUse.setCreateTime(DateUtils.getNowDate());
        return zcUseMapper.insertZcUse(zcUse);
    }

    /**
     * 修改资产用途（树）
     * 
     * @param zcUse 资产用途（树）
     * @return 结果
     */
    @Override
    public int updateZcUse(ZcUse zcUse)
    {
        ZcUse newParentUse = zcUseMapper.selectZcUseByUseId(zcUse.getParentId());
        ZcUse oldUse = zcUseMapper.selectZcUseByUseId(zcUse.getUseId());
        if (StringUtils.isNotNull(newParentUse) && StringUtils.isNotNull(oldUse)){
            String newAncestors = newParentUse.getAncestors() + "," + newParentUse.getUseId();
            String oldAncestors = oldUse.getAncestors();
            zcUse.setAncestors(newAncestors);
            updateUseChildren(zcUse.getUseId(), newAncestors, oldAncestors);
        }
        zcUse.setUpdateTime(DateUtils.getNowDate());
        int result = zcUseMapper.updateZcUse(zcUse);
        if (ZcxtConstants.USE_NORMAL.equals(zcUse.getStatus()) && StringUtils.isNotEmpty(zcUse.getAncestors())
                && !StringUtils.equals("0",zcUse.getAncestors())
        ){
            // 如果该分类是启用状态，则启用该分类的所有上级分类
            updateParentZcUseStatusNormal(zcUse);
        }

        return result;
    }

    /**
     * 批量删除资产用途（树）
     *
     * @param useId 需要删除的资产用途（树）主键
     * @return 结果
     */
    @Override
    public int deleteZcUseByUseIds(Long useId)
    {
        return zcUseMapper.deleteZcUseByUseIds(useId);
    }

    /**
     * 删除资产用途（树）信息
     * 
     * @param useId 资产用途（树）主键
     * @return 结果
     */
    @Override
    public int deleteZcUseByUseId(Long useId)
    {
        return zcUseMapper.deleteZcUseByUseId(useId);
    }

    /**
     * 根据ID查询所有子分类（正常状态）
     * @param useId 用途ID
     * @return 子分类数（正常状态）
     */
    @Override
    public int selectNormalChildrenZcUseByUseId(Long useId) {
        return zcUseMapper.selectNormalChildrenZcUseByUseId(useId);
    }

    @Override
    public int selectNormalChildrenZcUseById(Long useId) {
        return 0;
    }

    /**
     * 修改子元素关系
     *
     * @param useId 被修改的用途ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateUseChildren(Long useId, String newAncestors, String oldAncestors) {
        List<ZcUse> children = zcUseMapper.selectChildrenUseByUseId(useId);
        for (ZcUse child : children){
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors,newAncestors));
        }
        if (children.size()>0) {
            zcUseMapper.updateZcUseChildren(children);
        }
    }

    public void updateParentZcUseStatusNormal(ZcUse zcUse){
        String ancestors = zcUse.getAncestors();
        Long[] uses = Convert.toLongArray(ancestors);
        zcUseMapper.updateZcUseStatusNormal(uses);
    }

    @Override
    public boolean hasChildByUseId(Long useId){
        int result = zcUseMapper.hasChildByUseId(useId);
        return result > 0;
    }
}
